package com.barbeque.mall.service.mallmanagement;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.issue.MarketIssue;
import com.barbeque.mall.bean.issue.MarketIssueExample;
import com.barbeque.mall.bean.order.MarketOrder;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketIssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ycc
 * @date 2022/07/16 14:20:58
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    MarketIssueMapper issueMapper;

    @Override
    public CommonData<MarketOrder> list(BasePageInfo pageInfo, MarketIssue issue) {
        //开启分页
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());

        String sort = pageInfo.getSort();
        String order = pageInfo.getOrder();

        //拼接orderClause语句
        MarketIssueExample example = new MarketIssueExample();
        if (StringUtils.isEmpty(sort) && StringUtils.isEmpty(order)){
            //order by add_time desc
            example.setOrderByClause(sort+" "+order);
        }

        //拼接select语句
        MarketIssueExample.Criteria criteria = example.createCriteria();
        //显示未被删除的问答信息
        criteria.andDeletedEqualTo(false);
        //搜索问答
        String question = issue.getQuestion();
        if (!StringUtils.isEmpty(question)) {
            question = "%"+question+"%";
            criteria.andQuestionLike(question);
        }

        List<MarketIssue> issues = issueMapper.selectByExample(example);

        PageInfo<MarketIssue> info = new PageInfo<>(issues);

        return CommonData.data(info);
    }

    @Override
    public int update(MarketIssue issue) {
        //更改修改时间
        issue.setUpdateTime(new Date());

        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdEqualTo(issue.getId());
        //问答id不能更改
        issue.setId(null);
        //添加时间不能更改
        issue.setAddTime(null);

        return issueMapper.updateByExampleSelective(issue, issueExample);
    }

    @Override
    public int delete(MarketIssue issue) {
        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdEqualTo(issue.getId());
        //问答id不能更改
        issue.setId(null);
        //添加时间不能更改
        issue.setAddTime(null);
        //修改删除状态码
        issue.setDeleted(true);

        return issueMapper.updateByExampleSelective(issue, issueExample);
    }

    @Override
    public int create(MarketIssue issue) {
        //添加问答的时间
        issue.setAddTime(new Date());

        return issueMapper.insertSelective(issue);
    }
}
